﻿"use strict";

//const bootstrap = require("../lib/bootstrap/dist/js/bootstrap");

var connection = new signalR.HubConnectionBuilder().withUrl("/infoHub").build();
var currentPc = 0;
document.getElementById("write").disabled = true;
document.getElementById("run").disabled = true;

var mp = document.getElementById("m0");
mp.scrollIntoView();

connection.on("ReceiveMessage", function (cmd, msg) {
    if (cmd == "display") {
        var li = document.createElement("li");
        document.getElementById("messagesList").appendChild(li);
        li.textContent = `${msg}`;
    }
    if (cmd == "memory") {
        var data = msg.split(",");
        document.getElementById("m" + data[0]).value = "0x"+ data[1];
    }
    if (cmd == "register") {
        var data = msg.split(",");
        document.getElementById("x" + data[0]).value = "0x"+data[1];
        if (data[0] == "100") {//pc寄存器
            document.getElementById("m" + currentPc).style.border = "";
            currentPc = parseInt(data[1],16);
            document.getElementById("m" + currentPc).style.border = "medium solid #00FF00";
        }
    }
    if (cmd == "comment") {
        var data = msg.split("`");
        document.getElementById("c" + data[0]).innerHTML = data[1];
    }
    if (cmd == "translate") {
        document.getElementById("messageInput").value += msg +"\n";
    }
});

connection.start().then(function () {
    document.getElementById("write").disabled = false;
    document.getElementById("run").disabled = false;
    document.getElementById("x100").value = "0x00000000";
    document.getElementById("x0").value = "0x00000000";
    connection.invoke("SendMessage", "pc", "read").catch(function (err) {
        return console.error(err.toString());
    });
    connection.invoke("SendMessage", "rom", "read").catch(function (err) {
        return console.error(err.toString());
    });
}).catch(function (err) {
    return console.error(err.toString());
});

document.getElementById("write").addEventListener("click", function (event) {
    var message = document.getElementById("messageInput").value;
    var start = document.getElementById("startaddr").value;
    var istext = document.getElementById("istext").checked;
    connection.invoke("SendMessage", "write", start + "`" + istext+"`"+ message).catch(function (err) {
        return console.error(err.toString());
    });
    var modelie = document.getElementById("writewindow");
    var model = bootstrap.Modal.getInstance(modelie);
    model.hide();
    event.preventDefault();
});

document.getElementById("debug").addEventListener("click", function (event) {
    var debug = document.getElementById("debugaddr").value;
    connection.invoke("SendMessage", "debug", debug).catch(function (err) {
        return console.error(err.toString());
    });
    var modelie = document.getElementById("debugwindow");
    var model = bootstrap.Modal.getInstance(modelie);
    model.hide();
    event.preventDefault();
});

document.getElementById("run").addEventListener("click", function (event) {
    connection.invoke("SendMessage", "run", "").catch(function (err) {
        return console.error(err.toString());
    });
    event.preventDefault();
});

document.getElementById("runstep").addEventListener("click", function (event) {
    connection.invoke("SendMessage", "runstep", "").catch(function (err) {
        return console.error(err.toString());
    });
    event.preventDefault();
});

document.getElementById("pause").addEventListener("click", function (event) {
    connection.invoke("SendMessage", "pause", "").catch(function (err) {
        return console.error(err.toString());
    });
    event.preventDefault();
});

document.getElementById("reset").addEventListener("click", function (event) {
    connection.invoke("SendMessage", "reset", "").catch(function (err) {
        return console.error(err.toString());
    });
    window.location.reload();
    event.preventDefault();
});

document.getElementById("book").addEventListener("click", function (event) {
    var cmd = document.getElementById("bookcmd").value;
    var rs1 = document.getElementById("bookrs1").value;
    var rs2 = document.getElementById("bookrs2").value;
    var rd = document.getElementById("bookrd").value;
    var imm = document.getElementById("bookimm").value;

    connection.invoke("SendMessage", "book", `${cmd},${rs1},${rs2},${rd},${imm}`).catch(function (err) {
        return console.error(err.toString());
    });
    event.preventDefault();
});